package org.wheel.module.core.datetime.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.wheel.module.core.datetime.TimeSerializer;

import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * description: LocalTimeSerializer <br>
 * date: 2022/9/16 2:29 下午 <br>
 * author: hixx <br>
 * version: 1.0 <br>
 */
public final class LocalTimeToTimeSerializer extends JsonSerializer<LocalTime> {

    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(TimeSerializer.PATTERN_LOCAL_TIME);


    @Override
    public void serialize(LocalTime localTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(localTime != null) {
            jsonGenerator.writeString(dateTimeFormatter.format(localTime));
        }
    }
}
